package com.zeei.bfs.controller;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zeei.bfs.dao.SysLogDao;
import com.zeei.bfs.service.DeptinfoService;
import com.zeei.bfs.vo.DeptinfoVO;
import com.zeei.bfs.vo.OrgTreeVO;
import com.zeei.bfs.vo.QueryDeptParamVO;
import com.zeei.common.annotation.SysLog;
import com.zeei.common.constant.GlobalConstant;
import com.zeei.common.controller.BasicController;
import com.zeei.common.enums.LogType;
import com.zeei.common.algorithm.CryptographyUtil;
import com.zeei.common.utils.SessionInfoUtil;
import com.zeei.common.utils.StringUtil;
import com.zeei.common.utils.UserOperationLog;
import com.zeei.common.vo.ResultBasicVO;
import com.zeei.common.vo.ResultRowsVO;
import com.zeei.common.vo.TreeNodeVO;
import com.zeei.common.vo.UserVO;

@Controller
@RequestMapping("/bfs/dept")
public class DeptinfoController extends BasicController {

	@Resource
	private DeptinfoService deptinfoService;
	
	@Resource
	private SysLogDao syslogDao;

	@RequestMapping(value = "/valid", method = RequestMethod.POST)
	@ResponseBody
	public ResultBasicVO validDeptName(DeptinfoVO deptinfo) {
		return this.createResultBasicVO(deptinfoService.validDeptName(deptinfo), null);
	}

	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@ResponseBody
	public ResultRowsVO<TreeNodeVO<DeptinfoVO>> queryDept(QueryDeptParamVO queryDeptParam) {
		UserVO user = SessionInfoUtil.getSessionUserInfo();
		List<TreeNodeVO<DeptinfoVO>> list = deptinfoService.queryDeptinfo(queryDeptParam,user);
		return this.createResultRowsVO(list, true, null, list.size());
	}

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	@SysLog(text = "添加部门信息", type = LogType.Edit)
	public ResultBasicVO addDept(DeptinfoVO deptinfo,UserVO user) {
		syslogDao.insert(UserOperationLog.getSysLogVO(request, " 执行 添加"+deptinfo.getDeptName()+"部门信息 操作.", LogType.Add));
		Integer userId = SessionInfoUtil.getSessionUserId();
		if (userId == null) {
			return this.createResultBasicVO(true, null);
		}
		//管理员密码加密
		String loginpw=CryptographyUtil.EncryptMd5Hash(user.getLoginpw());
		user.setLoginpw(loginpw);

		deptinfo.setCreateId(userId);
		user.setCreateid(userId);
		boolean status=deptinfoService.addDeptinfo(deptinfo,user);
		return this.createResultBasicVO(status, null);
	}

	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	@ResponseBody
	@SysLog(text = "编辑部门信息", type = LogType.Edit)
	public ResultBasicVO editDept(DeptinfoVO deptinfo) {
		syslogDao.insert(UserOperationLog.getSysLogVO(request, " 执行 编辑"+deptinfo.getDeptName()+"部门信息 操作.", LogType.Edit));
		
		return this.createResultBasicVO(deptinfoService.editDept(deptinfo), null);
	}

	@RequestMapping(value = "/drop", method = RequestMethod.POST)
	@ResponseBody
	@SysLog(text = "删除部门信息", type = LogType.Edit)
	public ResultBasicVO dropDept(String deptIds) {
		
		List<String> list = StringUtil.splitToList(deptIds);
		String selfDeptId = SessionInfoUtil.getDeptId();
		if(existSelfDept(list, selfDeptId)){
//			deleteSelfDept(list,selfDeptId);
			return this.createResultBasicVO(false, "不可以删除自己部门!");
		}
		syslogDao.insert(UserOperationLog.getSysLogVO(request, " 执行 删除ID为"+deptIds+"的部门信息 操作.", LogType.Delete));
		
		return this.createResultBasicVO(deptinfoService.dropDept(list), null);
	}
	
	private void deleteSelfDept(List<String> deptIdList, String selfDeptId) {
		if(selfDeptId!=null && CollectionUtils.isNotEmpty(deptIdList)){
			Iterator<String> it = deptIdList.iterator();
			while (it.hasNext()){
				String deptId = it.next();
				if(selfDeptId.equals(deptId)){
					it.remove();
				}
			}
		}
	}

	private boolean existSelfDept(List<String> deptIdList, String selfDeptId) {
		if(StringUtil.isEmpty(selfDeptId)||CollectionUtils.isEmpty(deptIdList)){
			return false;
		}
		boolean existSelfDept=false;
		for(String deptId:deptIdList){
			if(selfDeptId.equals(deptId)){
				existSelfDept=true;
				break;
			}
		}
		return existSelfDept;
	}

	@RequestMapping(value = "/combobox", method = RequestMethod.POST)
	@ResponseBody
	public ResultRowsVO<TreeNodeVO<DeptinfoVO>> queryComboboxDept(DeptinfoVO deptinfo) {
		UserVO user = SessionInfoUtil.getSessionUserInfo();
		
		List<TreeNodeVO<DeptinfoVO>> resultTree=new ArrayList<TreeNodeVO<DeptinfoVO>>(); 
		String message="false";
		if(GlobalConstant.ADMIN_NAME.equalsIgnoreCase(user.getLoginname())){
			message="true";
//			TreeNodeVO<DeptinfoVO>  treeNode=new TreeNodeVO<DeptinfoVO>();
//			treeNode.setId("");
//			treeNode.setText("无上级部门");
//			resultTree.add(treeNode);
		}
		
		List<TreeNodeVO<DeptinfoVO>> deptTree = deptinfoService.queryDeptinfo(deptinfo);
		resultTree.addAll(deptTree);
		return this.createResultRowsVO(resultTree, true, message, resultTree.size()); 
	}
	
	@RequestMapping(value = "/query", method = RequestMethod.POST)
	@ResponseBody
	public List<DeptinfoVO> queryDeptById(Integer deptId) {
		return deptinfoService.queryDeptinfoById(deptId);
	}

	@RequestMapping(value = "/deptuser/treelist", method = RequestMethod.POST)
	@ResponseBody
	public ResultRowsVO<TreeNodeVO<OrgTreeVO>> queryDeptuserTreeList(Integer roleId) {
		List<TreeNodeVO<OrgTreeVO>> list = deptinfoService.queryDeptuserTreeList(roleId);
		return this.createResultRowsVO(list, true, null, list.size());
	}

	@RequestMapping(value = "/deptuser/add", method = RequestMethod.POST)
	@ResponseBody
	@SysLog(text = "添加部门用户关系", type = LogType.Edit)
	public ResultBasicVO addDeptuserRelation(Integer roleId, String[] ids) {
		Integer userId = SessionInfoUtil.getSessionUserId();
		syslogDao.insert(UserOperationLog.getSysLogVO(request, " 执行 添加"+StringUtils.join(ids, ",")+"部门用户关系 操作.", LogType.Add));
		
		return this.createResultBasicVO(deptinfoService.addDeptuserRelation(userId, roleId, ids), null);
	}

	@RequestMapping(value = "/treelist", method = RequestMethod.POST)
	@ResponseBody
	public ResultRowsVO<TreeNodeVO<OrgTreeVO>> queryDeptOrgTreeList() {
		List<TreeNodeVO<OrgTreeVO>> list = deptinfoService.queryDeptOrgTreeList();
		return this.createResultRowsVO(list, true, null, list.size());
	}

}
